const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

//noinspection JSUnresolvedFunction
module.exports = {
    entry: {
        app: './src/index.js',
        vendor: ['react', 'react-dom']
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].[chunkhash].js'
    },
    resolve: {
        alias: {
            'react': 'react/dist/react.js',
            'react-dom': 'react-dom/dist/react-dom.js',
        }
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    use: ['css-loader']
                })
            },
            {
                test: /\.(png|svg|jpg|gif)/,
                use: ['file-loader']
            }
        ]
    },
    plugins: [
        new ManifestPlugin(),
        new HtmlWebpackPlugin({
            title: 'Output Management',
            favicon: './src/favicon.ico'
        }),
        // new webpack.HotModuleReplacementPlugin(),
        // new webpack.optimize.UglifyJsPlugin({
        //     sourceMap: true
        // }),
        // new webpack.DefinePlugin({
        //     'process.env.NODE_ENV': JSON.stringify('production')
        // }),
        new ExtractTextPlugin('styles.css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            chunks: ['vendor']
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'manifest',
            minChunks: Infinity,
        }),
        // function () {
        //     this.plugin('done', stats => {
        //         require('fs').writeFileSync(path.join(__dirname, 'dist', 'stats.json'), JSON.stringify(stats.toJson()));
        //     });
        // },
    ],
    // devtool: 'inline-source-map',
    devServer: {
        hot: true,
        contentBase: path.resolve(__dirname, 'dist'),
        publicPath: '/'
    }
};